package com.fsxgt.datagrid.datax.reader.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.datax.plugin.rdbms.reader.Constant;
import com.fsxgt.datagrid.component.bo.CmpSyncOfflineBO;
import com.fsxgt.datagrid.datax.reader.ReaderBuilder;
import com.fsxgt.datagrid.ds.bo.DatasourceJdbcBO;
import com.fsxgt.datagrid.ds.component.QueryComponent;
import com.fsxgt.datagrid.ds.utils.ParamUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;

/**
 * 
 * @ClassName: RdbmsReaderBuilder
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-06-09 14:07:47
 */
@Component
public abstract class RdbmsReaderBuilder extends ReaderBuilder{
	
	@Autowired
	private QueryComponent queryComponent;

	public abstract String getName() ;

    public Map<String, Object> build(CmpSyncOfflineBO bo,Map params) throws Exception {
    	
    	DatasourceJdbcBO srcDatasource = queryComponent.getDatasource(bo.getSrcDatasourceId());
		
		
        //构建
        Map<String, Object> readerObj = Maps.newLinkedHashMap();
        readerObj.put("name", getName());
        Map<String, Object> parameterObj = Maps.newLinkedHashMap();
        Map<String, Object> connectionObj = Maps.newLinkedHashMap();

        parameterObj.put("username", srcDatasource.getJdbcUsername());
        parameterObj.put("password", srcDatasource.getJdbcPassword());
        
        parameterObj.put(Constant.FETCH_SIZE, bo.getFetchSize());
        
       
        
        //判断是否是 querySql
        if (StringUtils.isNotEmpty(bo.getSrcTableName())) {
        	
        	List srcColumns=new ArrayList();
     		bo.getColumnList().forEach(b->{
     			srcColumns.add(queryComponent.doConvertKeywordsColumn(srcDatasource.getDatasource(),b.getSrcColumnName()));
     		});
     		
     		parameterObj.put("column", srcColumns);
     		
            //判断是否有where
            connectionObj.put("table", ImmutableList.of(bo.getSrcTableName()));
            
            String where="";
            
            if(StringUtils.isNotEmpty(bo.getSrcIdExpression())) {
            	where+=" "+bo.getSrcIdExpression()+">${startId} and "+bo.getSrcIdExpression()+"<=${endId}";
            }
            
            if (StringUtils.isNotBlank(bo.getSrcWhereParam())) {
            	where+=" and "+ bo.getSrcWhereParam();
            }
            if(!where.equals("")) {
               parameterObj.put("where", where);
            }
        } else {
        	String srcSqlText=bo.getSrcSqlText();
            connectionObj.put("querySql", ImmutableList.of(srcSqlText));
        }
        
        
       
        
        
        parameterObj.put("splitPk",bo.getSplitPk());
        connectionObj.put("jdbcUrl", ImmutableList.of(srcDatasource.getJdbcUrl()));

        parameterObj.put("connection", ImmutableList.of(connectionObj));

        readerObj.put("parameter", parameterObj);

        return readerObj;
    }
    
    
    public static void main (String args[]) {
    	String s="1113 WHERE 4222";
    	System.out.println(s);
    


    }
  

    
    private static  String getIndexByRegex(String str,String pattern){

        Pattern r2 = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m2 = r2.matcher(str);
        if(m2.find()) {
            return m2.group(0);
        }  else {
            return null;
        }
    }
    
    
}
